import { ref, watch } from 'vue'
import type { Position } from '../types/chess'

interface Performance {
  wins: number
  losses: number
  averageMoveQuality: number
  lastGames: {
    result: 'win' | 'loss' | 'draw'
    moveQuality: number
  }[]
}

export function useAdaptiveAI() {
  const currentDifficulty = ref<'easy' | 'medium' | 'hard'>('medium')
  const performanceHistory = ref<Performance>({
    wins: 0,
    losses: 0,
    averageMoveQuality: 0,
    lastGames: []
  })

  // 评估移动质量（基于与最佳移动的差异）
  const evaluateMoveQuality = (
    playerMove: { from: Position, to: Position },
    bestMove: Position,
    board: (string | null)[][]
  ): number => {
    // 计算移动与最佳移动的距离
    const distance = Math.sqrt(
      Math.pow(playerMove.to.row - bestMove.row, 2) + 
      Math.pow(playerMove.to.col - bestMove.col, 2)
    )

    // 评估移动是否导致棋子被吃
    const isCapture = board[playerMove.to.row][playerMove.to.col] !== null
    const willBeCaptured = isPositionThreatened(playerMove.to, board)

    let quality = 1.0
    
    // 根据与最佳移动的距离调整质量
    quality -= distance * 0.1

    // 根据是否吃子调整质量
    if (isCapture) quality += 0.2
    if (willBeCaptured) quality -= 0.3

    return Math.max(0, Math.min(1, quality))
  }

  // 检查位置是否被威胁
  const isPositionThreatened = (
    position: Position,
    board: (string | null)[][]
  ): boolean => {
    // 实现检查位置是否被对方棋子威胁的逻辑
    return false
  }

  // 更新性能历史
  const updatePerformance = (
    result: 'win' | 'loss' | 'draw',
    moveQuality: number
  ) => {
    if (result === 'win') performanceHistory.value.wins++
    if (result === 'loss') performanceHistory.value.losses++

    performanceHistory.value.lastGames.push({ result, moveQuality })
    if (performanceHistory.value.lastGames.length > 5) {
      performanceHistory.value.lastGames.shift()
    }

    // 更新平均移动质量
    performanceHistory.value.averageMoveQuality = 
      performanceHistory.value.lastGames.reduce((sum, game) => sum + game.moveQuality, 0) /
      performanceHistory.value.lastGames.length

    adjustDifficulty()
  }

  // 调整难度
  const adjustDifficulty = () => {
    const { wins, losses, averageMoveQuality, lastGames } = performanceHistory.value
    
    // 计算最近的胜率
    const recentWinRate = lastGames
      .filter(game => game.result === 'win').length / lastGames.length

    // 根据胜率和移动质量调整难度
    if (recentWinRate > 0.6 && averageMoveQuality > 0.7) {
      // 玩家表现很好，提高难度
      if (currentDifficulty.value === 'easy') {
        currentDifficulty.value = 'medium'
      } else if (currentDifficulty.value === 'medium') {
        currentDifficulty.value = 'hard'
      }
    } else if (recentWinRate < 0.3 || averageMoveQuality < 0.4) {
      // 玩家表现不佳，降低难度
      if (currentDifficulty.value === 'hard') {
        currentDifficulty.value = 'medium'
      } else if (currentDifficulty.value === 'medium') {
        currentDifficulty.value = 'easy'
      }
    }
  }

  // 重置性能历史
  const resetPerformance = () => {
    performanceHistory.value = {
      wins: 0,
      losses: 0,
      averageMoveQuality: 0,
      lastGames: []
    }
    currentDifficulty.value = 'medium'
  }

  return {
    currentDifficulty,
    performanceHistory,
    evaluateMoveQuality,
    updatePerformance,
    resetPerformance
  }
} 